import processing.serial.*;
import controlP5.*;
import ddf.minim.*;
import ddf.minim.analysis.*;

/*****************************************
  colorArray[0]  = B10000000; //Black
  colorArray[1]  = B10010101; //White
  colorArray[2]  = B10000100; //Red
  colorArray[3]  = B10010100; //Yellow
  colorArray[4]  = B10010000; //Green
  colorArray[5]  = B10010001; //Teal
  colorArray[6]  = B10000001; //Blue
  colorArray[7]  = B10000101; //Purple
  colorArray[8]  = B11001000; //Dark Red
  colorArray[9]  = B11101000; //Dark Yellow
  colorArray[10] = B11010000; //Dark Green
  colorArray[11] = B11100010; //Dark Teal
  colorArray[12] = B11000010; //Dark Blue
  colorArray[13] = B11001010; //Dark Purple
  colorArray[14] = B11101010; //Dark White
*******************************************/
Minim minim;
AudioInput in;
BeatDetect beat;
BeatListener bl;
ControlP5 controlP5;
MyControlListener Listener;
MyControlListener redListener;
MyControlListener greenListener;
MyControlListener blueListener;
MyControlListener sensListener;
Serial myPort;  // Create object from Serial class

int r;
int g;
int b;
int sens;
int modsens;
int mycolor;        // Data received from the serial port
void setup() 
{
  size(700, 700);
  frameRate(30);
  //minim stuff
  minim = new Minim(this);
  in = minim.getLineIn(Minim.STEREO, 2048);   
  beat = new BeatDetect(in.bufferSize(), in.sampleRate());
  bl = new BeatListener(beat, in);

  textFont(createFont("Helvetica", 16));
  textAlign(CENTER);
  controlP5 = new ControlP5(this);
  controlP5.addSlider("BPM",0,300,128,120,330,100,10);
  controlP5.addSlider("red",0,255,0,200,50,100,10);
  controlP5.addSlider("green",0,255,0,200,150,100,10);
  controlP5.addSlider("blue",0,255,0,200,250,100,10);

  redListener   = new MyControlListener();
  greenListener = new MyControlListener();
  blueListener  = new MyControlListener();
  sensListener  = new MyControlListener();
  
  beat.setSensitivity(469);
  sens = 128;
 
  controlP5.controller("red").addListener(redListener);
  controlP5.controller("green").addListener(greenListener);
  controlP5.controller("blue").addListener(blueListener);
  controlP5.controller("BPM").addListener(sensListener);

  boolean redKICK    = false;
  boolean redSNARE   = false;
  boolean redHAT     = false;
  boolean greenKICK  = false;
  boolean greenSNARE = false;
  boolean greenHAT   = false;
  boolean blueKICK   = false;
  boolean blueSNARE  = false;
  boolean blueHAT    = false;

  println(Serial.list());
  String portName = Serial.list()[1];
  myPort = new Serial(this, portName, 115200);

}

void draw() {

  sens = sensListener.col;
  modsens = 60000 / (sens +1);

  beat.setSensitivity(modsens);

  background(204);
  if (keyPressed) { //white
    if(key == 'r'){
      fill(255,255,255);                    
      myPort.write(1);
    }
    else if(key == 'q'){ //red
      fill(255,0,0);
      myPort.write(2);
    }
    else if(key == 'a'){ //yellow
      fill(255,0,255);
      myPort.write(3);
    }
    else if(key == 'w'){ //green
      fill(0,255,0);
      myPort.write(4);
    }
    else if(key == 's'){ //teal
      fill(0,255,255);
      myPort.write(5);
    }
    else if(key == 'e'){ //blue
      fill(0,0,255);                    
      myPort.write(6);
    }
    else if(key == 'd'){ //purple
      fill(255,0,255);                    
      myPort.write(7);
    }
  }
  else {                
    fill(0,0,0);;
    myPort.write(0);
  }
  rect(50, 50, 100, 100);         // Draw a square
}

boolean mouseOverRect() { // Test if mouse is over square
  return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
}

class MyControlListener implements ControlListener {
  int col;
  public void controlEvent(ControlEvent theEvent) {
    col = (int)theEvent.controller().value();
  }

}

class BeatListener implements AudioListener
{
  private BeatDetect beat;
  private AudioInput source;

  BeatListener(BeatDetect beat, AudioInput source)
  {
    this.source = source;
    this.source.addListener(this);
    this.beat = beat;
  }

  void samples(float[] samps)
  {
    beat.detect(source.mix);
  }

  void samples(float[] sampsL, float[] sampsR)
  {
    beat.detect(source.mix);
  }
}

void stop()
{
  // always close Minim audio classes when you are finished with them
  in.close();
  // always stop Minim before exiting
  minim.stop();
  // this closes the sketch
  super.stop();
}
